<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: SubscriberDAO.cfc 1133 2007-04-05 01:07:20Z mpwoodward $

Notes:
--->

<cfcomponent displayname="SubscriberDAO" 
		output="false" 
		extends="SubscriberDAO"
		hint="SubscriberDAO for MachBlog (MySQL)">
			
	<cffunction name="init" access="public" output="false" returntype="SubscriberDAO" hint="Constructor">
		<cfreturn this />
	</cffunction>
	
	<cffunction name="create" access="public" output="false" returntype="void" hint="Creates a new subscriber record">
		<cfargument name="subscriber" type="Subscriber" required="true" />
		
		<cfset var checkSubscriber = "" />
		<cfset var insertSubscriber = "" />
		
		<cftry>
			<cfquery name="checkSubscriber" datasource="#getDatasource().getDsn()#" 
					username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
				SELECT subscriber_id 
				FROM machblog_subscriber 
				WHERE subscriber_email = <cfqueryparam value="#arguments.subscriber.getEmail()#" cfsqltype="cf_sql_varchar" 
														maxlength="100" />
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>
		
		<cfif isQuery(checkSubscriber) AND checkSubscriber.RecordCount GT 0>
			<cfthrow type="application" message="email exists" detail="email exists">
		<cfelse>
			<cftry>
				<cfquery name="insertSubscriber" datasource="#getDatasource().getDsn()#" 
						username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
					INSERT INTO machblog_subscriber (
						subscriber_id, 
						subscriber_email, 
						verify_id, 
					<cfif arguments.subscriber.getAudit().getCreatedById() NEQ "">
						created_by_id, 
					</cfif>
						dt_created, 
						ip_created, 
						is_active
					) VALUES (
						<cfqueryparam value="#arguments.subscriber.getSubscriberId()#" cfsqltype="cf_sql_char" maxlength="35" />, 
						<cfqueryparam value="#arguments.subscriber.getEmail()#" cfsqltype="cf_sql_varchar" maxlength="100" />, 
						<cfqueryparam value="#arguments.subscriber.getVerifyId()#" cfsqltype="cf_sql_char" maxlength="35" />, 
					<cfif arguments.subscriber.getAudit().getCreatedById() NEQ "">
						<cfqueryparam value="#arguments.subscriber.getAudit().getCreatedById()#">, 
					</cfif>
						<cfqueryparam value="#arguments.subscriber.getAudit().getDtCreated()#" cfsqltype="cf_sql_bigint" />, 
						<cfqueryparam value="#arguments.subscriber.getAudit().getIpCreated()#" cfsqltype="cf_sql_varchar" maxlength="15" />, 
						<cfqueryparam value="#arguments.subscriber.getAudit().getIsActive()#" cfsqltype="cf_sql_bit" />
					)
				</cfquery>
				<cfcatch type="database">
					<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
				</cfcatch>
			</cftry>
		</cfif>
	</cffunction>
	
	<cffunction name="read" access="public" output="false" returntype="void" hint="Populates a subscriber bean">
		<cfargument name="subscriber" type="Subscriber" required="true" />
		
		<cfset var getSubscriber = "" />
		<cfset var cbName = "" />
		<cfset var mbName = "" />
		<cfset var dtModified = 0 />
		<cfset var isActive = 0 />

		<cftry>
			<cfquery name="getSubscriber" datasource="#getDatasource().getDsn()#" 
						username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
				SELECT 	machblog_subscriber.*, 
						cb.first_name AS cbfn, 
						cb.last_name AS cbln, 
						mb.first_name AS mbfn, 
						mb.last_name AS mbln 
				FROM 	machblog_subscriber 
				LEFT OUTER JOIN machblog_user AS cb 
					ON machblog_subscriber.created_by_id = cb.user_id 
				LEFT OUTER JOIN machblog_user AS mb 
					ON machblog_subscriber.modified_by_id = mb.user_id 
				WHERE 	machblog_subscriber.subscriber_id = <cfqueryparam value="#arguments.subscriber.getSubscriberId()#" 
																		cfsqltype="cf_sql_char" maxlength="35" />
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>
		
		<cfif isQuery(getSubscriber) AND getSubscriber.RecordCount EQ 1>
			<cfif getSubscriber.cbfn NEQ "">
				<cfset cbName = getSubscriber.cbfn & " " & getSubscriber.cbln />
			</cfif>
			
			<cfif getSubscriber.mbfn NEQ "">
				<cfset mbName = getSubscriber.mbfn & " " & getSubscriber.mbln />
			</cfif>
			
			<cfif getSubscriber.dt_modified NEQ "">
				<cfset dtModified = getSubscriber.dt_modified />
			</cfif>
			
			<cfif getSubscriber.is_active NEQ "">
				<cfset isActive = getSubscriber.is_active />
			</cfif>
			
			<cfset arguments.subscriber.init(getSubscriber.subscriber_id, getSubscriber.subscriber_email, getSubscriber.verify_id) />
			<cfset arguments.subscriber.getAudit().init(getSubscriber.created_by_id, cbName, getSubscriber.dt_created, getSubscriber.ip_created, 
														getSubscriber.modified_by_id, mbName, dtModified, getSubscriber.ip_modified, 
														isActive) />
		</cfif>
	</cffunction>
	
	<cffunction name="update" access="public" output="false" returntype="void" hint="Updates a subscriber record">
		<cfargument name="subscriber" type="Subscriber" required="true" />
		
		<cfset var checkEmail = "" />
		<cfset var updateSubscriber = "" />
		
		<!--- check to make sure that if the email they're updating to isn't already in use --->
		<cftry>
			<cfquery name="checkEmail" datasource="#getDatasource().getDsn()#" 
					username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
				SELECT subscriber_id 
				FROM machblog_subscriber 
				WHERE subscriber_email = <cfqueryparam value="#arguments.subscriber.getEmail()#" cfsqltype="cf_sql_varchar" maxlength="100" /> 
				AND subscriber_id != <cfqueryparam value="#arguments.subscriber.getSubscriberId()#" cfsqltype="cf_sql_char" maxlength="35" />
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>
		
		<cfif isQuery(checkEmail) AND checkEmail.RecordCount GT 0>
			<cfthrow type="application" message="email exists" detail="email exists" />
		<cfelse>
			<cftry>
				<cfquery name="updateSubscriber" datasource="#getDatasource().getDsn()#" 
						username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
					UPDATE 	machblog_subscriber 
					SET 	subscriber_email = <cfqueryparam value="#arguments.subscriber.getEmail()#" cfsqltype="cf_sql_varchar" 
															maxlength="100" />, 
							verify_id = <cfqueryparam value="#arguments.subscriber.getVerifyId()#" cfsqltype="cf_sql_char" 
														maxlength="35" />, 
							modified_by_id = <cfqueryparam value="#arguments.subscriber.getAudit().getModifiedById()#" cfsqltype="cf_sql_char" 
															maxlength="35" />, 
							dt_modified = <cfqueryparam value="#arguments.subscriber.getAudit().getDtModified()#" cfsqltype="cf_sql_bigint" />, 
							ip_modified = <cfqueryparam value="#arguments.subscriber.getAudit().getIpModified()#" cfsqltype="cf_sql_varchar" 
														maxlength="15" />, 
							is_active = <cfqueryparam value="#arguments.subscriber.getAudit().getIsActive()#" cfsqltype="cf_sql_bit" /> 
					WHERE 	subscriber_id = <cfqueryparam value="#arguments.subscriber.getSubscriberId()#" cfsqltype="cf_sql_char" maxlength="35" />
				</cfquery>
				<cfcatch type="database">
					<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
				</cfcatch>
			</cftry>
		</cfif>
	</cffunction>
	
	<cffunction name="delete" access="public" output="false" returntype="void" hint="Deletes a subscriber record">
		<cfargument name="subscriber" type="Subscriber" required="true" />
		
		<cfset var deleteSusbcriber = "" />
		
		<cftry>
			<cfquery name="deleteSubscriber" datasource="#getDatasource().getDsn()#" 
					username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
				DELETE FROM machblog_subscriber 
				WHERE subscriber_id = <cfqueryparam value="#arguments.subscriber.getSubscriberId()#" cfsqltype="cf_sql_char" 
													maxlength="35" />
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>
	</cffunction>
</cfcomponent>